<?php
if (! defined ( 'BASEPATH' ))
    exit ( 'No direct script access allowed' );

class Payment extends Controller {
    public function __construct() {
        parent::__construct ();
        $this->_data = array ();
        $this->load->model ( 'registrasi_model' );
        isController('kasir','payment');
    }
    protected function _view($template, $data = array(), $result = false) {
        if ($data && is_array ( $data )) {
            return $this->load->view ( $template, $data, $result );
        }
        return $this->load->view ( $template, $this->_data, $result );
    }
    public function index() {
        redirect ( 'klinik/payment/view' );
    }

    public function view() {
    	if (! isAccess ( 'kasir', 'payment', 'view'))
            redirect (); 
        $array = $this->uri->uri_to_assoc ( 3, array ('id', 'pasienId', 'frdate', 'todate', 'action', 'page' ) );
        $array['page'] = (int) $array['page'];
        
        //Variable
        $this->load->model('pasien_model');
        $this->_data['listPasien'] = $this->pasien_model->getList(100, 0);
        
        //Uri
        $pasienIdVal = $this->input->post('pasienId') ? $this->input->post('pasienId') : 'all';
        $array['pasienId'] = get_data($array,'pasienId') ? get_data($array,'pasienId') : $pasienIdVal;
        
        $frdate = $this->input->post('frdate') ? $this->input->post('frdate') : 'all';
        $array['frdate'] = get_data($array,'frdate') ? get_data($array,'frdate') : $frdate;

        $todate = $this->input->post('todate') ? $this->input->post('todate') : 'all';
        $array['todate'] = get_data($array,'todate') ? get_data($array,'todate') : $todate;

        //Where
        $where = array();
        $where['rmStatus'] = array( 'Selesai' );
        if( $array['pasienId'] != 'all' )
        	$where['pasienId'] = $array['pasienId'];
        if( $array['frdate'] != 'all' )
        	$where['frdate'] = $array['frdate'];
        if( $array['todate'] != 'all' )
        	$where['todate'] = $array['todate'];	
    
        $total = $this->registrasi_model->getCount($where);
        
        $this->load->library('pagination');
        if (($array['page'] + 1) > $total)
            $array['page'] = 0;
        $limit = $this->config->item('klinik_limit');
        $config = $this->config->item('paging');
       	$config['base_url'] = site_url('klinik/payment/view/pasienId/'.$array['pasienId'].'/frdate/'.$array['frdate'].'/todate/'.$array['todate'].'/page/');
        $config['uri_segment'] = 10;
        $config['total_rows'] = $total;
        $config['per_page'] = $limit;
        
        $this->pagination->initialize($config);
        $this->_data['paging'] =  $this->pagination->create_links();
        
        $dataView = $this->registrasi_model->getList($where, 1000, 0);
        $this->_data['dataView'] = $dataView;
        $this->_data['uri_to_assoc'] = $array;

        $this->_view ( 'klinik/paymentView' );
    }
    
	public function printView() {
    	if (! isAccess ( 'kasir', 'payment', 'printview'))
            redirect (); 
  		$array = $this->uri->uri_to_assoc ( 3, array ('pasienId', 'frdate', 'todate') );
  		if ( !$array ['pasienId'] || !$array ['frdate'] || !$array ['todate'] )
            show_404 ();
            
        //Data General
        $where = array();
        $where['rmStatus'] = array( 'Selesai' );
        if( $array['pasienId'] != 'all' )
        	$where['pasienId'] = $array['pasienId'];
        if( $array['frdate'] != 'all' )
        	$where['frdate'] = $array['frdate'];
        if( $array['todate'] != 'all' )
        	$where['todate'] = $array['todate'];

        $dataView = $this->registrasi_model->getList($where, 1000, 0);
        $this->_data['dataView'] = $dataView;
	    
        //Template PDF
        $content = '<page backtop="14mm" backbottom="14mm" pagegroup="new">
				    <page_header>
					<table style="width:100%;">
					    <tr>
							<td style="width:70%;">'.$this->lang->line('global_title').'</td>
							<td style="width:30%;">Laporan Pembayaran Kasir</td>
					    </tr>
				        <tr>
							<td>'.$this->lang->line('global_title_cab').'</td>';
        
						if ($array['frdate'] != 'all' && $array['todate'] == 'all') {	
							$content .= '<td>Tanggal : '.date('d M Y', strtotime($array['frdate'])).'</td>';
						} elseif ($array['frdate'] != 'all' && $array['todate'] != 'all') {	
							$content .= '<td>Tanggal : '.date('d M Y', strtotime($array['frdate'])).' - '.date('d M Y', strtotime($array['todate'])).'</td>';
						} else {
							$content .= '<td>Semua Transaksi</td>';
						}
						
			$content .= '</tr>
					     <tr>
							<td>'.$this->lang->line('global_alamat_cab').'</td>
							<td></td>
					     </tr>
					</table>
				    </page_header>
				    <page_footer>
					<table style="width: 100%;">
					    <tr>
						<td style="text-align: right">
						    Page : [[page_cu]]/[[page_nb]]
						</td>
					    </tr>
					</table>
				    </page_footer>
				</page><br />';

	    $content .= '<table border="1" style="width:100%;">
				    <thead style="text-align:center;height:100px;">
					<tr height="50">
					    <th style="width:5%">No</th>
					    <th style="width:10%">Kode RM</th>
					    <th style="width:10%">Tanggal</th>
					    <th style="width:25%;">Nama</th>
					    <th style="width:10%">Jenkel</th>
					    <th style="width:10%">Usia</th>
					    <th style="width:3%"></th>
					    <th style="width:25%">Biaya</th>
					</tr>
				    </thead>
				    <tbody style="text-align:center;">';
		
	    $no=1;
	    $totalPayment = 0;
	    if($dataView) foreach ($dataView as $key=>$row) {
			$content .= '<tr>
							<td>'.$no.'</td>
							<td>'.$row['rmKode'].'</td>';
							
							if( $key > 0 ) {
								if( mydate($dataView[$key-1]['rmTglMulai']) == mydate($row['rmTglMulai']) ) {
									$content .= '<td></td>';
								} else {
									$content .= '<td>'.mydate($row['rmTglMulai']).'</td>';
								}
								
							} else {
								$content .= '<td>'.mydate($row['rmTglMulai']).'</td>';
							}
							
				$content .= '<td align="left">'.$row['pasienNama'].'</td>
							 <td>'.$row['pasienJenKel'].'</td>
							 <td>'.getUsia( get_data($row,'pasienTglLahir') ).'</td>
							 <td>Rp. </td>
							 <td align="right">'.number_format($row['rmTotalBiaya']).'</td>
	    		         </tr>';
			
			$totalPayment += $row['rmTotalBiaya'];
			$no++;	
	    }

	    $content .= '<tfoot>
		    			 <tr style="font-size:15px;">
		    			 	 <td></td>
		    			 	 <td></td>
		    			 	 <td></td>
		    			 	 <td></td>
		    			 	 <td colspan="2" align="center"><b>TOTAL BIAYA</b></td>
		    			 	 <td align="center">Rp.</td>
		    			 	 <td align="right"><b>'.number_format($totalPayment).'</b></td>
		    			 </tr>
	    			 </tfoot>
	    			 
	    			 </tbody>
	                 </table>';

	    //Convert to PDF
	    $this->load->library('html2pdf/html2pdf');
	    $html2pdf = new HTML2PDF('L', 'A4');
	    $html2pdf->pdf->SetDisplayMode('fullpage');
	    $html2pdf->writeHTML($content, FALSE);
	    
		if ($array['frdate'] != 'all' && $array['todate'] == 'all') {	
			$html2pdf->Output( 'Laporan_Pembayaran_'.date('dmY', strtotime($array['frdate'])).'.pdf' );
		} elseif ($array['frdate'] != 'all' && $array['todate'] != 'all') {	
			$html2pdf->Output( 'Laporan_Pembayaran_'.date('dmY', strtotime($array['frdate'])).'_'.date('dmY', strtotime($array['todate'])).'.pdf' );
		} else {
			$html2pdf->Output( 'Laporan_Pembayaran_Semua.pdf' );
		}
	    
    }
    
	public function today() {
    	if (! isAccess ( 'kasir', 'payment', 'today'))
            redirect (); 
        $array = $this->uri->uri_to_assoc ( 3, array ('id', 'status', 'action', 'page' ) );
        $array['page'] = (int) $array['page'];

        //Status
        $listStatus = array('Tunggu', 'Selesai');
        $this->_data['listStatus'] = $listStatus;
        
        //Uri
        $array['status'] = get_data($array,'status') ? get_data($array,'status') : 'all';
        
        //Where
        $where = array();
        $where['rmTglMulai'] = date('Y-m-d');
        if( get_data($array,'status') != 'all'  )
        	$where['rmStatus'] = array( $array['status'] );

        $dataView = $this->registrasi_model->getList($where, 1000, 0);
        $this->_data['dataView'] = $dataView;
        
        $this->_data['uri_to_assoc'] = $array;
        $this->_view ( 'klinik/paymentToday' );
    }
    
	public function printToday() {
    	if (! isAccess ( 'kasir', 'payment', 'printtoday'))
            redirect (); 
  
        //Data General
        $where = array();
        $where['rmTglMulai'] = date('Y-m-d');
        $where['rmStatus'] = 'Selesai';

        $dataView = $this->registrasi_model->getList($where, 1000, 0);
        $this->_data['dataView'] = $dataView;
	    
        //Template PDF
        $content = '<page backtop="14mm" backbottom="14mm" pagegroup="new">
				    <page_header>
					<table style="width:100%;">
					    <tr>
							<td style="width:70%;">'.$this->lang->line('global_title').'</td>
							<td style="width:30%;">Laporan Pembayaran Kasir</td>
					    </tr>
				        <tr>
							<td>'.$this->lang->line('global_title_cab').'</td>
							<td>Tanggal : '.date('d M Y').'</td>
					    </tr>
					    <tr>
							<td>'.$this->lang->line('global_alamat_cab').'</td>
							<td></td>
					    </tr>
					</table>
				    </page_header>
				    <page_footer>
					<table style="width: 100%;">
					    <tr>
						<td style="text-align: right">
						    Page : [[page_cu]]/[[page_nb]]
						</td>
					    </tr>
					</table>
				    </page_footer>
				</page><br />';

	    $content .= '<table border="1" style="width:100%;">
				    <thead style="text-align:center;height:100px;">
					<tr height="50">
					    <th style="width:5%">No</th>
					    <th style="width:10%">Kode RM</th>
					    <th style="width:35%">Nama</th>
					    <th style="width:10%">Jenkel</th>
					    <th style="width:10%">Usia</th>
					    <th style="width:3%"></th>
					    <th style="width:25%">Biaya</th>
					</tr>
				    </thead>
				    <tbody style="text-align:center;">';
		
	    $no=1;
	    $totalPayment = 0;
	    if($dataView) foreach ($dataView as $row) {
			$content .= '<tr>
							<td>'.$no.'</td>
							<td>'.$row['rmKode'].'</td>
							<td align="left">'.$row['pasienNama'].'</td>
							<td>'.$row['pasienJenKel'].'</td>
							<td>'.getUsia( get_data($row,'pasienTglLahir') ).'</td>
							<td>Rp. </td>
							<td align="right">'.number_format($row['rmTotalBiaya']).'</td>
	    		         </tr>';
			
			$totalPayment += $row['rmTotalBiaya'];
			$no++;	
	    }

	    $content .= '<tfoot>
		    			 <tr style="font-size:15px;">
		    			 	 <td></td>
		    			 	 <td></td>
		    			 	 <td></td>
		    			 	 <td colspan="2" align="center"><b>TOTAL BIAYA</b></td>
		    			 	 <td align="center">Rp.</td>
		    			 	 <td align="right"><b>'.number_format($totalPayment).'</b></td>
		    			 </tr>
	    			 </tfoot>
	    			 
	    			 </tbody>
	                 </table>';

	    //Convert to PDF
	    $this->load->library('html2pdf/html2pdf');
	    $html2pdf = new HTML2PDF('L', 'A4');
	    $html2pdf->pdf->SetDisplayMode('fullpage');
	    $html2pdf->writeHTML($content, FALSE);
	    $html2pdf->Output( 'Laporan_Pembayaran_'.date('dmY').'.pdf' );
    }
    
    //Cetak Pembayaran Pasien
	public function cetak() {
        $array = $this->uri->uri_to_assoc ( 3, array ('id') );
        if (! $array ['id'])
            show_404 ();
            
        //Load
        $this->load->helper('inflector');
        $this->load->model( array('alat_model', 'pemeriksaan_model') );
            
        //Data General
        $dataPasien = $this->registrasi_model->get ( $array ['id'] );
        $this->_data['dataPasien'] = $dataPasien;    
        if (! $dataPasien)
            show_404 ();
            
        //Data Alat
        $dataAlat = $this->alat_model->getListRM( array('rmId'=>$array['id']) );    
        $this->_data['dataAlat'] = $dataAlat;
        
        //Data Alat
        $dataPemeriksaan = $this->pemeriksaan_model->getListRM( array('rmId'=>$array['id']) );    
        $this->_data['dataPemeriksaan'] = $dataPemeriksaan;

        //Template PDF
        $content = '<page backtop="14mm" backbottom="14mm" pagegroup="new">
				    <page_header>
					<table style="width:100%;">
					    <tr>
							<td style="width:70%;">'.$this->lang->line('global_title').'</td>
							<td style="width:30%;">Bukti Pembayaran Pemeriksaan</td>
					    </tr>
				        <tr>
							<td>'.$this->lang->line('global_title_cab').' </td>
							<td>'.date('d/m/Y H:i:s').'</td>
					    </tr>
					    <tr>
							<td>'.$this->lang->line('global_alamat_cab').'</td>
							<td></td>
					    </tr>
					</table>
				    </page_header>
				    <page_footer>
					<table style="width: 100%;">
					    <tr>
						<td style="text-align: right">
						    Page : [[page_cu]]/[[page_nb]]
						</td>
					    </tr>
					</table>
				    </page_footer>
				</page><br />';
		
        //Header
        $content .= '<br /><br />
        			<table>
		                <tr>
		                    <td width="150">Nama</td>
		                    <td width="25"> : </td>
		                    <td width="565">
		                        <b>'.get_data($dataPasien,'pasienNama').'</b>
		                    </td>
		                    
		                    <td width="100">Tanggal</td>
		                    <td width="25"> : </td>
		                    <td>
		                        <b>'.get_data($dataPasien,'rmTglMulai').'</b>
		                    </td>
		                </tr>
		                <tr>
		                    <td>Jenkel</td>
		                    <td> : </td>
		                    <td>
		                        <b>'.get_data($dataPasien,'pasienJenKel').'</b>
		                    </td>
		                    
		                    <td>Kode RJ</td>
		                    <td> : </td>
		                    <td>
		                        <b>'.get_data($dataPasien,'rmKode').'</b>
		                    </td>
		                </tr>
		                <tr>
		                    <td>Usia</td>
		                    <td> : </td>
		                    <td>
		                        <b>'.getUsia( get_data($dataPasien,'pasienTglLahir')).'</b>
		                    </td>
		                    
		                    <td></td>
		                    <td></td>
		                    <td></td>
		                </tr>
		            </table>
		            <br />

		            <table>
		            	<tr>
		                    <td width="150">Keluhan / Gejala</td>
		                    <td width="25"> : </td>
		                    <td width="500">
		                        <b>'.get_data($dataPasien,'rmKeluhan').'</b>
		                    </td>
		                </tr>
		                <tr>
		                    <td>Diagnosa</td>
		                    <td> : </td>
		                    <td>
		                        <b>'.get_data($dataPasien,'rmDiagnosa').'</b>
		                    </td>
		                </tr>
		                <tr>
		                    <td>Keterangan</td>
		                    <td> : </td>
		                    <td>
		                        <b>'.get_data($dataPasien,'rmKeterangan').'</b>
		                    </td>
		                </tr>
		            </table>
		            <br /><br />';
        
		if($dataAlat || $dataPemeriksaan) {

	        $content .= "<table width='100%' border='1'>
			            	<tr height='40' style='font-size:13px;'>
			                    <th style='width:30px;text-align:center;'><b>NO</b></th>
			                    <th style='width:600px;text-align:center;'><b>NAMA</b></th>
			                    <th style='width:20px;text-align:center;'></th>
			                    <th style='width:300px;text-align:center;'><b>HARGA</b></th>
			                </tr>
			                
			                <!-- Alat -->
			                <tr height='30' border='0'>
			                    <td></td>
			                    <td><b>Alat</b></td>
			                    <td></td>
			                    <td></td>
			                </tr>";
			                
			                $noAlat=1;
			                foreach($dataAlat as $alat) {
				            $content .= "<tr height='30'>
						                    <td style='text-align:center;'>".$noAlat."</td>
						                    <td>".get_data($alat,'alatNama')."</td>
						                    <td>Rp.</td>
						                    <td style='text-align:right;'>".number_format(get_data($alat,'rmAlatHargaJual'))."</td>
						                </tr>";
				           				$noAlat++;   
			                }
			
			            	$content .= "<tr height='30'>
						                    <td></td>
						                    <td></td>
						                    <td></td>
						                    <td>&nbsp;</td>
						                </tr>
						                <tr height='30'>
						                    <td></td>
						                    <td><b>Pemeriksaan</b></td>
						                    <td></td>
						                    <td></td>
						                </tr>";
			                
			                $noPemeriksaan=$noAlat;
			                foreach($dataPemeriksaan as $pemeriksaan) {
				            $content .= "<tr>
						                    <td style='text-align:center;'>".$noPemeriksaan."</td>
						                    <td>".get_data($pemeriksaan,'pemeriksaanNama')."</td>
						                    <td>Rp.</td>
						                    <td style='text-align:right;'>".number_format(get_data($pemeriksaan,'rmPemeriksaanHarga'))."</td>
						                 </tr>";
				            
				            			$noPemeriksaan++;  
			                }
							
			                $content .= "<tr height='40' style='font-size:15px;'>
							                    <td></td>
							                    <td align='right'><b>TOTAL BIAYA</b></td>
							                    <td>Rp.</td>
							                    <td style='text-align:right;'><b>".number_format(get_data($dataPasien,'rmTotalBiaya'))."</b></td>
							            </tr>
						            	</table>";
	            
        }
	
	    //Convert to PDF
	    $this->load->library('html2pdf/html2pdf');
	    $html2pdf = new HTML2PDF('L', 'A4');
	    $html2pdf->pdf->SetDisplayMode('fullpage');
	    $html2pdf->writeHTML($content, FALSE);
	    $html2pdf->Output( underscore($dataPasien['pasienNama']).'_'.$dataPasien['rmKode'].'_'.date('dmY').'.pdf' );
    }
    
}